import { request } from '../../request/index'
import { async } from '../../lib/runtime/runtime'
import { formatTime } from '../../utils/util'
Page({
  data: {
    tabs: [
      { id: 0, name: "全部订单", isactive: true },
      { id: 1, name: "待付款", isactive: false },
      { id: 2, name: "待收货", isactive: false },
      { id: 3, name: "退货/退款", isactive: false },
    ],
    orders: []//订单数据
  },


  onLoad: function (options) {

  },


  onShow(options) {

    //1获取小程序中页面栈-数组 长度最大是10页面
    //数组中索引最大的就是当前页面
    let pages = getCurrentPages();
    let currentpage = pages[pages.length - 1]
    const { type } = currentpage.options
    this.changeTitle(type - 1)
    this.getorders(type)

  },
  changeTitle(index) {
    let { tabs } = this.data
    tabs.forEach((v, i) => i === index ? v.isactive = true : v.isactive = false)
    this.setData({
      tabs
    })
  },
  indexchange(e) {
    const { index } = e.detail
    this.changeTitle(index)
    this.getorders(index + 1)
  },
  async getorders(type) {//根据type获取订单数据 type=1 全部订单   type=2  待付款订单   type=3  待收货订单
    const token = { data: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo" }
    wx.setStorageSync('token', token);
    const header = { Authorization: token.data }
    const res = await request({ url: 'my/orders/all', data: { type }, header: header })
    console.log(res.data.message.orders)
    this.setData({
      orders: res.data.message.orders.map(v => ({ ...v, create_time_cn: (new Date(v.create_time * 1000).toLocaleString()) }))   //对日期进行处理
    })


  }


})